//
//  NetworkTools.swift
//  NetWorkingTools
//
//  Created by WangPengHua on 15/12/7.
//  Copyright © 2015年 WangPengHua. All rights reserved.
//

import UIKit
import AFNetworking
/// 请求格式的枚举
enum HZRequestMethod: String{
    case GET = "GET"
    case POST = "POST"
}
// MARK: 网络工具管理类
class NetworkTools: AFHTTPSessionManager {
/// 1>创建工具类的单列对象
    static let sharedTools: NetworkTools = {
        let tools = NetworkTools()
        //当系统返回的响应不符合要求时,可以在这里插入一个响应的格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        return tools
    }()
    
}

//MARK: - 封装 AFN 网络方法, 发送请求
extension NetworkTools{
    /// 发送网络请求
    ///
    /// - parameter requestMethod: GET/POST
    /// - parameter URLString:     URLString
    /// - parameter parameters:    参数字典["name":"wangwu"]
    /// - parameter finished:      完成回调(回调闭包)
    func request(requestMethod: HZRequestMethod, URLString: String,parameters: [String: AnyObject]?,finished: (result: AnyObject?, error: NSError?) -> ()){
/// 发送请求代码重构
        //定义闭包,成功的回调
        let success = { (task: NSURLSessionDataTask,result: AnyObject) -> () in
            
            finished(result: result, error: nil)
        }
        //定义闭包,失败的回调
        let failure = {(task: NSURLSessionDataTask?, error: NSError) -> () in
            
            print(error)
            
            finished(result: nil, error: error)
            
        }
        if requestMethod == HZRequestMethod.GET{
            //发送GET请求
            GET(URLString, parameters: parameters, success: success, failure: failure)
        }else if requestMethod == HZRequestMethod.POST{
            //发送POST请求
            POST(URLString, parameters: parameters, success: success, failure: failure)
            
            
            
            //模拟提交
        }
     }
}


